package br.com.srv.web;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.srv.componentes.veiculo.service.VeiculoService;
import br.com.srv.security.SecurityPrincipal;
import br.com.srv.util.DateHelper;

public final class IndexAction extends BaseAction {

	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		List<Map<String, String>> lastPosition = null;
		try {
			SecurityPrincipal user = (SecurityPrincipal) request.getUserPrincipal();
			List veiculos = user.getVeiculos();
			
			VeiculoService veiculoService =
				(VeiculoService)getBeanSpring(VeiculoService.SERVICE_NAME);
			
			lastPosition = veiculoService.buscarUltimasPosicaoVeiculos(veiculos);
			
			Date today = DateHelper.getToday();
			String lastHour = DateHelper.parseString(DateHelper.DEFAULT_HOUR_PATTERN, DateHelper.addHour(today, -1));
			request.setAttribute("horaInicial", lastHour);
			request.setAttribute("horaFinal", "23:59:59");
			request.setAttribute("pontos", lastPosition);
		} catch (Exception ex) {
			saveActionError(request, ex);
		}
		return mapping.findForward("indexaction");
	}
}
